﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplLogic.ApplClasses;

namespace TCPClient.UI.UserControls
{
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class OneShiftManagerUserControl : UserControl
    {
        private DateTime m_StartTime;
        private DateTime m_EndTime;
        private int m_NumberInDay;
        List<SingleWorkerInShiftUserControl> m_SingleWorkerInShiftUserControls = new List<SingleWorkerInShiftUserControl>(20);

        public OneShiftManagerUserControl()
        {
            InitializeComponent();
        }

        //public CheckedListBox CheckedListBox
        //{
        //    get { return checkedListBox; }
        //}

        public void SetData(DateTime startTime, DateTime endTime, int numberInDay)
        {
            m_StartTime = startTime;
            m_EndTime = endTime;
            m_NumberInDay = numberInDay;
            initTheSingleWorkerControls();
        }

        private void initTheSingleWorkerControls()
        {
            for (int i = 0; i < 20; i++)
            {
                SingleWorkerInShiftUserControl singleWorker = new SingleWorkerInShiftUserControl();
                singleWorker.BorderStyle = BorderStyle.FixedSingle;
                singleWorker.Dock = DockStyle.Top;
                singleWorker.Visible = false;
                m_SingleWorkerInShiftUserControls.Add(singleWorker);
                Controls.Add(singleWorker);
            }
            linkLabelShiftDetails.Visible = true;

        }

        public void SetCheckBoxToListenToToAllWorkers(CheckBox checkBox1, CheckBox checkBox2)
        {
            foreach (var workerUc in m_SingleWorkerInShiftUserControls)
            {
                workerUc.SetCheckBoxToListenTo(checkBox1, checkBox2);
            }
        }

        public int NumberInDay
        {
            get { return m_NumberInDay; }
            set { m_NumberInDay = value; }
        }

        public DateTime StartTime
        {
            get { return m_StartTime; }
            set { m_StartTime = value; }
        }

        public DateTime EndTime
        {
            get { return m_EndTime; }
            set { m_EndTime = value; }
        }

        public void DrawRequestedWorkers(List<ApplSingleRequest> workers)
        {
            linkLabelShiftDetails.Visible = true;
            int index = 0;
            foreach (var applRequest in workers)
            {
                Color colorToset = applRequest.Type == eType.WANTED ? Color.GreenYellow : Color.Red;
                m_SingleWorkerInShiftUserControls[index].SetTextInLabel(
                    string.Format("{0}.{1}", applRequest.ApplWorker.PersonalDetails.FirstName,
                                  applRequest.ApplWorker.PersonalDetails.LastName[0]), colorToset);
                m_SingleWorkerInShiftUserControls[index].Visible = true;
                m_SingleWorkerInShiftUserControls[index].Worker = applRequest.ApplWorker;
                index++;
            }
        }

        public void DrawRegisteredWorkers(List<ApplSingleResult> workersResult)
        {
            linkLabelShiftDetails.Visible = true;
            int index = 0;
            foreach (var applResult in workersResult)
            {
                Color colorToset = new Color();
                switch (applResult.Step)
                {
                    case eResult.FIRST:
                        colorToset = Color.GreenYellow;
                        break;
                    case eResult.SECOND:
                        colorToset = Color.Turquoise;
                        break;
                    case eResult.THIRD:
                        colorToset = Color.Red;
                        break;
                    default:
                        colorToset = Color.Peru;
                        break;
                }
                BackColor = SystemColors.ActiveCaption;
                m_SingleWorkerInShiftUserControls[index].SetTextInLabel(
                    string.Format("{0}.{1}", applResult.ApplWorker.PersonalDetails.FirstName,
                                  applResult.ApplWorker.PersonalDetails.LastName[0]), colorToset);
                m_SingleWorkerInShiftUserControls[index].Visible = true;
                m_SingleWorkerInShiftUserControls[index].Worker = applResult.ApplWorker;
                index++;
            }
        }

        public void CleanAll()
        {
            foreach (var singleWorkerInShiftUserControl in m_SingleWorkerInShiftUserControls)
            {
                singleWorkerInShiftUserControl.Label.Text = "";
                singleWorkerInShiftUserControl.Visible = false;
                singleWorkerInShiftUserControl.Worker = null;
                singleWorkerInShiftUserControl.IsClicked = false;
                singleWorkerInShiftUserControl.BackColor = SystemColors.ActiveCaption;
            }
            //linkLabelShiftDetails.Visible = false;
        }

        public void RegisterToClickEventsOfTheSingleWorkersLabelUserControls(EventHandler handler)
        {
            foreach (var singleWorkerInShiftUserControl in m_SingleWorkerInShiftUserControls)
            {
                singleWorkerInShiftUserControl.WorkerLabelClicked += handler;
            }
        }

        public void RegisterToClickEventsOfTheSingleWorkersUserControls(EventHandler handler)
        {
            foreach (var singleWorkerInShiftUserControl in m_SingleWorkerInShiftUserControls)
            {
                singleWorkerInShiftUserControl.WorkerClicked += handler;
            }
        }

        private void linkLabelShiftDetails_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShiftDetailsForm sdf = new ShiftDetailsForm(m_StartTime, m_EndTime);
            sdf.ShowDialog();
        }
    }
}
